package com.property.manage.service.parking;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.guardchina.framework.util.utils.PhoneUtil;
import com.property.manage.dao.parking.ParkingMapper;
import com.property.manage.model.parking.ParkingEntity;
import com.property.manage.model.parking.ParkingVo;
import com.property.manage.model.village.Village;
import com.property.manage.service.village.VillageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.math.BigDecimal;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Component
@Slf4j
public class ParkingService {
    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private VillageService villageService;


    public ParkingVo findParking(Integer id) {
        ParkingVo parkingVo = new ParkingVo();
        ParkingEntity entity = parkingMapper.selectById(id);
        if (entity==null){
            return parkingVo;
        }
        BeanUtils.copyProperties(entity,parkingVo);
        Village village = villageService.findVillage(entity.getVillageId());
        if (village==null){
            throw new BusinessException("车位所属小区信息错误",String.format("小区id[%s]",entity.getVillageId()));
        }
        parkingVo.setVillageName(village.getName());
        return parkingVo;
    }

    public CustomPage<ParkingVo> page(Long pageNo, Long pageSize, Integer villageId,
                                               String number,
                                               String licensePlate,
                                              Integer type) {
        Integer status=0;
        if ("-1".equals(licensePlate)){
            status=1;
        }
        if (licensePlate!=null&&!"".equals(licensePlate) && !"-1".equals(licensePlate)){
            status=2;
        }
        Page<ParkingVo> page = new Page<>(pageNo, pageSize);
        IPage<ParkingVo> village = parkingMapper.getParking(page, villageId,number,licensePlate,type,status);
        return new CustomPage<>(village);
    }


    public void add(Integer type,String number,String phone,
                    Integer villageId, String licensePlate, String userName,
                    BigDecimal money
    ) {
        if (querNumber(number,villageId)){
            throw new BusinessException("编号已经存在，请勿重复添加",String.format("编号[%s]",number));
        }
        checkData(type, phone, userName,money);
        ParkingEntity entity = new ParkingEntity();
        entity.setLicensePlate(licensePlate);
        entity.setMoney(money);
        entity.setNumber(number);
        entity.setPhone(phone);
        entity.setVillageId(villageId);
        entity.setUserName(userName);
        entity.setType(type);
        parkingMapper.insert(entity);
    }

    private void checkData(Integer type, String phone, String userName,BigDecimal money) {
        if (type!=1){
            if (StringUtils.isBlank(phone)){
                throw new BusinessException("车位出租或出售，请添加联系方式");
            }
            if (StringUtils.isBlank(userName)){
                throw new BusinessException("车位出租或出售，请添加联系人");
            }
            if (money==null){
                throw new BusinessException("车位出租或出售，请添加金额");
            }
        }
    }

    public void update(Integer id,Integer type,String number,String phone,
                        String licensePlate, String userName,
                       BigDecimal money) {
        ParkingVo parking = this.findParking(id);
        if (parking==null){
            throw new BusinessException("id错误",String.format("车位id[%s]",id));
        }
        if (!number.equals(parking.getNumber())){
            if (querNumber(number,parking.getVillageId())){
                throw new BusinessException("编号已经存在，请勿重复添加",String.format("编号[%s]",number));
            }
        }
        checkData(type, phone, userName,money);
        UpdateWrapper<ParkingEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("type", type);
        wrapper.set("number", number);
       if (type==1){
           //车位空值 清空车辆数据
           wrapper.set("phone", null);
           wrapper.set("license_plate", null);
           wrapper.set("user_name", null);
           wrapper.set("money", null);
       }else {
           wrapper.set(PhoneUtil.isMobile(phone),"phone", phone);
           wrapper.set("license_plate", licensePlate);
           wrapper.set("user_name", userName);
           wrapper.set("money", money);
       }
        wrapper.eq("id", id);
       parkingMapper.update(null, wrapper);
    }

    public void delete(Integer id) {
        parkingMapper.deleteById(id);
    }

    public boolean querNumber(String number,Integer villageId){
        QueryWrapper<ParkingEntity> qw=new QueryWrapper<>();
        qw.eq("number",number);
        qw.eq("village_id",villageId);
        return parkingMapper.selectOne(qw)!=null;
    }


    public Integer getCount(Integer villageId){
        return parkingMapper.getCount(villageId);
    }
}
